<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务权重级别" prop="weight" label-width="120px">
        <el-select v-model="queryParams.weight" placeholder="请选择任务权重级别" clearable size="small">
          <el-option label="紧急任务" value="0" />
          <el-option label="高" value="1" />
          <el-option label="中" value="2" />
          <el-option label="低" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务执行状态" prop="processStatus" label-width="120px">
        <el-select v-model="queryParams.processStatus" placeholder="请选择任务执行状态" clearable size="small">
          <el-option label="待执行" value="-1" />
          <el-option label="可执行" value="0" />
          <el-option label="正在执行" value="1" />
          <el-option label="执行完成" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-waves="['system:info:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <el-table-column label="任务权重级别" align="center" prop="weight">
        <template slot-scope="{row}">
          <el-tag>
            <template v-if="row.weight === 0">
              紧急任务
            </template>
            <template v-else-if="row.weight === 1">
              高
            </template>
            <template v-else-if="row.weight === 2">
              中
            </template>
            <template v-else-if="row.weight === 3">
              低
            </template>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务执行状态" align="center" prop="processStatus">
        <template slot-scope="{row}">
          <el-tag>
            <template v-if="row.processStatus === -1">
              待执行
            </template>
            <template v-else-if="row.processStatus === 0">
              可执行
            </template>
            <template v-else-if="row.processStatus === 1">
              正在执行
            </template>
            <template v-else-if="row.processStatus === 2">
              执行完成
            </template>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务完成度" align="center" prop="proportion">
        <template slot-scope="{row}">
          <el-tag>
            <template v-if="row.processStatus === -1">
              待执行
            </template>
            <template v-else-if="row.processStatus === 0">
              可执行
            </template>
            <template v-if="row.processStatus === 1">
              正在执行
            </template>
            <template v-else-if="row.processStatus === 2">
              执行完成
            </template>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="zhiXing(scope.row)"
          >执行</el-button>
          <el-button
            v-waves="['system:info:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >任务设计</el-button>
          <el-button
            v-waves="['system:info:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="name" label-width="120px">
          <el-input v-model="form.name" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务权重级别" prop="weight" label-width="120px">
          <el-select v-model="form.weight" placeholder="请选择任务权重级别" clearable size="small">
            <el-option label="紧急任务" value="0" />
            <el-option label="高" value="1" />
            <el-option label="中" value="2" />
            <el-option label="低" value="3" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, apiTaskProcessTaskId } from '@/api/task/info'
import waves from '@/directive/waves'

export default {
  name: 'Info',
  directives: { waves },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务信息表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        weight: null,
        processStatus: null,
        proportion: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请填写任务名称', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '请选择任务权重级别', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询任务信息列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        weight: null,
        processStatus: 0,
        status: 0,
        total: null,
        processTotal: null,
        proportion: null,
        errorTotal: null,
        executeTotal: null,
        completeExecuteTotal: null,
        successInsertTotal: null,
        successUpdateTotal: null,
        successDeleteTotal: null,
        errorAddTotal: null,
        errorLogicTotal: null,
        completeTime: null,
        grandTotalTime: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加任务信息'
    },
    /** 任务设计 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      const name = row.name
      this.$router.push({ path: '/taskEdit/edit', query: { id, name }})
      getInfo(id).then(response => {
        console.log(response)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除任务名称"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `system_info.xlsx`)
    },
    zhiXing(row) {
      const ids = row.id || this.ids
      apiTaskProcessTaskId(ids).then(res => {
        console.log(res)
      })
    }
  }
}
</script>
